package 刷题.无头双向链表实现;

import java.util.List;

public class LinkedList {
    public ListNode head;//头结点
    public ListNode last;//尾结点
    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head =node;
            this.last = node;
        }else{
            node.next = this.head;//node的next指向head
            this.head.prev = node;//head的prev指向node
            this.head = node;//更新头结点为插入结点
        }
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
            this.last = node;
        }else {
            this.last.next = node;//尾结点的last指向node
            node.prev = this.last;//node的prev指向last
            this.last = node;//更新尾结点为node
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index, int data) {
        if(index < 0 || index > size()){
            throw new RuntimeException("下标不合法");
        }
        if(index == 0){
            addFirst(data);//头插
            return true;
        }
        if(index == size()){
            addLast(data);//尾插
            return true;
        }
        ListNode node = new ListNode(data);//链表node
        ListNode cur = this.head;//头结点head
        int count = 0;
        while (count < index){
            cur = cur.next;
            count++;
        }
        node.next = cur;
        node.prev = cur.prev;
        cur.prev = node;
        node.prev.next = node;
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public ListNode remove(int key) {
        //遍历链表找到关键字为key结点，然后进行删除
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                //判断是不是头结点
                if(cur == this.head){
                    this.head = this.head.next;
                    this.head.prev = null;
                }else{//不是头结点
                    //判断是不是尾结点
                    if(cur.next == null){
                        //尾删
                        this.last = cur.prev;
                    }else{
                        //中间结点
                        cur.next.prev = cur.prev;
                    }
                }
                return this.head;
            }
            cur = cur.next;
        }
        return this.head;
    }

    //删除所有值为key的节点
    public ListNode removeAllKey(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            //找到相同的
            if (cur.val == key) {
                //头结点
                if(cur == head){
                    this.head = this.head.next;
                    this.head.prev = null;
                }else{
                    //尾结点
                    if(cur.next == null){
                        this.last = cur.prev;
                    }else{
                        //中间结点
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
        return this.head;
    }

    //得到单链表的长度
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            count ++;
            cur = cur.next;
        }
        return count;
    }
    //打印链表
    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    //清空链表
    public void clear() {
        ListNode cur = this.head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        last = null;
    }
}
